{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a73222c",
   "metadata": {},
   "source": [
    "## 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43bfd515",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   1    6   65  352 1281 3626 8641]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.array([0,1,2,3,4,5,6])\n",
    "y = x**5+4*x**3+1\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8630083f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   5.   59.  287.  929. 2345. 5015.]\n"
     ]
    }
   ],
   "source": [
    "f1 = []\n",
    "\n",
    "for i in range(len(y)-1):\n",
    "    f1.append((y[i+1]-y[i])/(x[i+1]-x[i]))\n",
    "\n",
    "f1 = np.array(f1)\n",
    "np.set_printoptions(suppress=True)\n",
    "print(f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "01981fff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  27.  114.  321.  708. 1335.]\n"
     ]
    }
   ],
   "source": [
    "f2 = []\n",
    "\n",
    "for i in range(len(f1)-1):\n",
    "    f2.append((f1[i+1]-f1[i])/(x[i+2]-x[i]))\n",
    "\n",
    "\n",
    "f2 = np.array(f2)\n",
    "print(f2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e67447f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 29.  69. 129. 209.]\n"
     ]
    }
   ],
   "source": [
    "f3 = []\n",
    "\n",
    "for i in range(len(f2)-1):\n",
    "    f3.append((f2[i+1]-f2[i])/(x[i+3]-x[i]))\n",
    "\n",
    "f3 = np.array(f3)\n",
    "\n",
    "print(f3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "814c588c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10. 15. 20.]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "f4 = []\n",
    "\n",
    "for i in range(len(f3)-1):\n",
    "    f4.append((f3[i+1]-f3[i])/(x[i+4]-x[i]))\n",
    "\n",
    "f4 = np.array(f4)\n",
    "print(f4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "49f0496b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1.]\n"
     ]
    }
   ],
   "source": [
    "f5 = []\n",
    "\n",
    "for i in range(len(f4)-1):\n",
    "\n",
    "    f5.append((f4[i+1]-f4[i])/(x[i+5]-x[i]))\n",
    "\n",
    "f5 = np.array(f5)\n",
    "\n",
    "print(f5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c2367bd",
   "metadata": {},
   "source": [
    "## 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a39fdead",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.          0.5        -3.16666667]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([0,1,1.5,2])\n",
    "\n",
    "g0 = np.array([3,3,3.25,5/3])\n",
    "\n",
    "g1 = []\n",
    "\n",
    "for i in range(len(g0)-1):\n",
    "    g1.append((g0[i+1]-g0[i])/(x[i+1]-x[i]))\n",
    "\n",
    "g1 = np.array(g1)\n",
    "\n",
    "print(g1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "11c56f95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.33333333 -3.66666667]\n"
     ]
    }
   ],
   "source": [
    "g2 = []\n",
    "for i in range(len(g1)-1):\n",
    "    g2.append((g1[i+1]-g1[i])/(x[i+2]-x[i]))\n",
    "g2 = np.array(g2)\n",
    "print(g2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "75ab2c27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2.]\n"
     ]
    }
   ],
   "source": [
    "g3 = []\n",
    "for i in range(len(g2)-1):\n",
    "    g3.append((g2[i+1]-g2[i])/(x[i+3]-x[i]))\n",
    "\n",
    "g3 = np.array(g3)\n",
    "print(g3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5083c8c8",
   "metadata": {},
   "source": [
    "## P268 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a165e561",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.99629402 1.10363832 0.53672153]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.array([[2, 0, 2/3],\n",
    "              [0, 2/3, 0],   \n",
    "              [2/3, 0, 2/5]],dtype=float)\n",
    "b = np.array([np.e-1/np.e, 2/np.e, np.e-5/np.e], dtype=float)\n",
    "\n",
    "x = np.linalg.solve(A, b)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8604720d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 3. 1.]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([[1, 1/2, 1/3],\n",
    "              [1/2, 1/3, 1/4],   \n",
    "              [1/3, 1/4, 1/5]],dtype=float)\n",
    "b = np.array([23/6,9/4,97/60], dtype=float)\n",
    "\n",
    "x = np.linalg.solve(A, b)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "625794c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[14.64655172  2.86206897 -1.62931034]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([[4, 0, 20],\n",
    "              [0, 20, 56],   \n",
    "              [20, 56, 164]],dtype=float)\n",
    "b = np.array([26,-34,186], dtype=float)\n",
    "\n",
    "x = np.linalg.solve(A, b)\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
